package mth.rmclusterer;

import java.io.File;

public class Evaluation {
	/*
	 * In dieser Klasse werden die 10 zusammengehörigen Clusteranalysen einer masterImp Lösung
	 * in einer FileGroup zusammengefasst. Mit diesen Gruppen werden die VotedRunResults,
	 * also die per Mehrheitsentscheid aufgebauten Lösungen, für jede FileGroup erzeugt.
	 */
	private FileGroup[] fileGroup;
	private VotedRunResult[] votedRuns; //cluster assignments per fileGroup
	private int realK;
	
	Evaluation(String rK) {
		fileGroup = new FileGroup[0];
		votedRuns = new VotedRunResult[0];
		realK = Integer.parseInt(rK);
	}
	
	
	
	private void buildFileGroups() {
		if (fileGroup==null)
			fileGroup=new FileGroup[0];
		
		File directory = new File(Manager.SOURCE_PATH_TO_VOTE);
//		File[] files = directory.listFiles();
		String[] filenames = directory.list();
		boolean breaker = false;
		for (int a=0; a<filenames.length; a++) {
			if (fileGroup.length==0) {
				fileGroup = new FileGroup[1];
				fileGroup[0] = new FileGroup(); //filenames[a].split("_")[2], filenames[a].split("_")[7]
				fileGroup[0].setMember(Manager.addStringToArray(fileGroup[0].getMember(), filenames[a]));
				fileGroup[0].getDistMeasure();
				fileGroup[0].getKValue();
				fileGroup[0].getQValue();
				fileGroup[0].getRealQValue();
				fileGroup[0].getNQValue();
				fileGroup[0].getPairCount();
				
			} else {
				for (int b=0; b<fileGroup.length && !breaker; b++) {
					if (!fileGroup[b].fileGroupContains(filenames[a])) {
						if (filenames[a].split("_")[2].equals(fileGroup[b].getQValue()) && filenames[a].split("_")[7].equals(fileGroup[b].getDistMeasure()) && 
								filenames[a].split("_")[4].equals(fileGroup[b].getKValue())) {
							fileGroup[b].setMember(Manager.addStringToArray(fileGroup[b].getMember(), filenames[a]));
							breaker=true;
						} else if (b==fileGroup.length-1 && !breaker && !fileGroup[b].fileGroupContains(filenames[a])) {
							fileGroup=fileGroup[b].addFileGroupToArray(fileGroup, new FileGroup());
							fileGroup[fileGroup.length-1].setMember(Manager.addStringToArray(fileGroup[fileGroup.length-1].getMember(), filenames[a]));
							fileGroup[fileGroup.length-1].getDistMeasure();
							fileGroup[fileGroup.length-1].getKValue();
							fileGroup[fileGroup.length-1].getQValue();
							fileGroup[0].getRealQValue();
							fileGroup[0].getNQValue();
							fileGroup[0].getPairCount();
							breaker=true;
						}
					} else {
						breaker=true;
					}
				}
				breaker = false;
			}
		}
	}
	
	public void consolidateRuns() {
		buildFileGroups();
		for (int a=0; a<fileGroup.length; a++) {
			if (votedRuns==null || votedRuns.length==0) {
				votedRuns = new VotedRunResult[1];
				votedRuns[0]=new VotedRunResult(fileGroup[a].getMember(),fileGroup[a].getQValue(),fileGroup[a].getDistMeasure(), fileGroup[a].getKValue(),
						fileGroup[0].getRealQValue(), fileGroup[0].getNQValue(), fileGroup[0].getPairCount(), realK);
			} else {
				VotedRunResult[] helper = new VotedRunResult[votedRuns.length+1];
				for (int b=0; b<votedRuns.length; b++) {
					helper[b]=votedRuns[b];
				}
				helper[votedRuns.length]=new VotedRunResult(fileGroup[a].getMember(),fileGroup[a].getQValue(),fileGroup[a].getDistMeasure(), fileGroup[a].getKValue(),
						fileGroup[a].getRealQValue(), fileGroup[a].getNQValue(), fileGroup[a].getPairCount(), realK);
				votedRuns = new VotedRunResult[helper.length];
				for (int b=0; b<helper.length; b++) {
					votedRuns[b]=helper[b];
				}
			}
		}
	}
	
	public VotedRunResult[] getVotedRuns() {
		return votedRuns;
	}
}
